package cn.ultrarisk.scadmin.manager.biz.zookeeper.entity;

import cn.ultrarisk.mybatis.extension.annotation.Table;
import org.apache.zookeeper.data.Stat;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * ZK节点实体类
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/14
 * @since V1.0
 */
@Table(table = "zk_node", idColumn = ZkNode.ID, autoIncrement = true)
public class ZkNode implements Serializable {
    private static final long serialVersionUID = -4209014902804649653L;

    public final static String ID = "id";
    public final static String PATH = "path";
    public final static String PARENT_ID = "parent_id";
    public final static String BACKUP_ID = "backup_id";
    public final static String CZXID = "czxid";
    public final static String MZXID = "mzxid";
    public final static String PZXID = "pzxid";
    public final static String CTIME = "ctime";
    public final static String MTIME = "mtime";
    public final static String VERSION = "version";
    public final static String CVERSION = "cversion";
    public final static String AVERSION = "aversion";
    public final static String EPHEMERAL_OWNER = "ephemeral_owner";
    public final static String NUM_CHILDREN = "num_children";
    public final static String DATA_LENGTH = "data_length";
    public final static String DATA = "data";

    public static final Map<String, String> COLUMN_PROPERTY_MAP = new LinkedHashMap<String, String>();

    static {
        COLUMN_PROPERTY_MAP.put(ID, "id");
        COLUMN_PROPERTY_MAP.put(PATH, "path");
        COLUMN_PROPERTY_MAP.put(PARENT_ID, "parentId");
        COLUMN_PROPERTY_MAP.put(BACKUP_ID, "backupId");
        COLUMN_PROPERTY_MAP.put(CZXID, "czxid");
        COLUMN_PROPERTY_MAP.put(MZXID, "mzxid");
        COLUMN_PROPERTY_MAP.put(PZXID, "pzxid");
        COLUMN_PROPERTY_MAP.put(CTIME, "ctime");
        COLUMN_PROPERTY_MAP.put(MTIME, "mtime");
        COLUMN_PROPERTY_MAP.put(VERSION, "version");
        COLUMN_PROPERTY_MAP.put(CVERSION, "cversion");
        COLUMN_PROPERTY_MAP.put(AVERSION, "aversion");
        COLUMN_PROPERTY_MAP.put(EPHEMERAL_OWNER, "ephemeralOwner");
        COLUMN_PROPERTY_MAP.put(NUM_CHILDREN, "numChildren");
        COLUMN_PROPERTY_MAP.put(DATA_LENGTH, "dataLength");
        COLUMN_PROPERTY_MAP.put(DATA, "data");
    }

    public ZkNode() {
    }

    public ZkNode(Stat stat) {
        this.setCzxid(stat.getCzxid());
        this.setMzxid(stat.getMzxid());
        this.setPzxid(stat.getPzxid());
        this.setCtime(stat.getCtime());
        this.setMtime(stat.getMtime());
        this.setVersion(stat.getVersion());
        this.setCversion(stat.getCversion());
        this.setAversion(stat.getAversion());
        this.setEphemeralOwner(stat.getEphemeralOwner());
        this.setNumChildren(stat.getNumChildren());
        this.setDataLength(stat.getDataLength());
    }

    /**
     * 编号
     */
    private Integer id;
    /**
     * 路径
     */
    private String path;
    /**
     * 父节点id
     */
    private Integer parentId;
    /**
     * 备份id
     */
    private Integer backupId;
    /**
     * 创建事务id
     */
    private Long czxid;
    /**
     * 修改事务id
     */
    private Long mzxid;
    /**
     * 子节点创建事务id
     */
    private Long pzxid;
    /**
     * 创建时间
     */
    private Long ctime;
    /**
     * 最后修改时间
     */
    private Long mtime;
    /**
     * 数据变更版本号
     */
    private Integer version;
    /**
     * 子节点变更版本号
     */
    private Integer cversion;
    /**
     * acl变更版本号
     */
    private Integer aversion;
    /**
     * 临时节点会话id
     */
    private Long ephemeralOwner;
    /**
     * 子节点数
     */
    private Integer numChildren;
    /**
     * 字节数
     */
    private Integer dataLength;
    /**
     * 内容
     */
    private String data;
    /**
     * 父节点
     */
    private ZkNode parent;
    /**
     * 子节点
     */
    private List<ZkNode> children;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public Integer getBackupId() {
        return backupId;
    }

    public void setBackupId(Integer backupId) {
        this.backupId = backupId;
    }

    public Long getCzxid() {
        return czxid;
    }

    public void setCzxid(Long czxid) {
        this.czxid = czxid;
    }

    public Long getMzxid() {
        return mzxid;
    }

    public void setMzxid(Long mzxid) {
        this.mzxid = mzxid;
    }

    public Long getPzxid() {
        return pzxid;
    }

    public void setPzxid(Long pzxid) {
        this.pzxid = pzxid;
    }

    public Long getCtime() {
        return ctime;
    }

    public void setCtime(Long ctime) {
        this.ctime = ctime;
    }

    public Long getMtime() {
        return mtime;
    }

    public void setMtime(Long mtime) {
        this.mtime = mtime;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Integer getCversion() {
        return cversion;
    }

    public void setCversion(Integer cversion) {
        this.cversion = cversion;
    }

    public Integer getAversion() {
        return aversion;
    }

    public void setAversion(Integer aversion) {
        this.aversion = aversion;
    }

    public Long getEphemeralOwner() {
        return ephemeralOwner;
    }

    public void setEphemeralOwner(Long ephemeralOwner) {
        this.ephemeralOwner = ephemeralOwner;
    }

    public Integer getNumChildren() {
        return numChildren;
    }

    public void setNumChildren(Integer numChildren) {
        this.numChildren = numChildren;
    }

    public Integer getDataLength() {
        return dataLength;
    }

    public void setDataLength(Integer dataLength) {
        this.dataLength = dataLength;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public ZkNode getParent() {
        return parent;
    }

    public void setParent(ZkNode parent) {
        this.parent = parent;
    }

    public List<ZkNode> getChildren() {
        return children;
    }

    public void setChildren(List<ZkNode> children) {
        this.children = children;
    }

    public void addChild(ZkNode child) {
        if (this.children == null) {
            this.children = new ArrayList<ZkNode>();
        }
        this.children.add(child);
    }

    public List<ZkNode> getChildrenByLevel(int level) {
        List<ZkNode> childNodes = new ArrayList<ZkNode>();
        if (level == 0) {
            childNodes.add(this);
        } else if (level > 0 && this.numChildren > 0) {
            for (ZkNode child : this.children) {
                childNodes.addAll(child.getChildrenByLevel(level - 1));
            }
        }
        return childNodes;
    }

    public String getRelativePath() {
        if (this.path != null && this.path.length() > 1) {
            String[] arr = this.path.split("/");
            if (arr.length > 0) {
                return arr[arr.length - 1];
            }
        }
        return this.path;
    }
}